package com.fzb.ftp;

import java.io.File;
import java.util.Scanner;

public class FtpRequestImpl implements IFtpRequest {

    private static boolean wait = false;
    private FtpSession ftpSession;

    public FtpRequestImpl(FtpSession ftpSession) {
        this.ftpSession = ftpSession;
    }

    @Override
    public String inputUserName() {
        String userName;
        if (ftpSession.getFtpUser().getUserName() != null) {
            userName = ftpSession.getFtpUser().getUserName();
        } else {
            Scanner sc = new Scanner(System.in);
            System.out.println("input userName");
            userName = sc.nextLine();
            sc.close();
        }
        return "USER " + userName;
    }

    @Override
    public String inputPassword() {
        String password;
        if (ftpSession.getFtpUser().getPassword() != null) {
            password = ftpSession.getFtpUser().getPassword();
        } else {
            Scanner sc = new Scanner(System.in);
            System.out.println("input password");
            password = sc.nextLine();
            sc.close();
        }
        return "PASS " + password;
    }

    @Override
    public void sendCmd() {
        Scanner sc = new Scanner(System.in);
        while (getLock()) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.print("> ");
        String cmd = sc.nextLine().trim();
        sendCmd(cmd);
    }

    public void sendCmd(String cmd) {
        while (getLock()) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (cmd.startsWith("get ")) {
            String fileName = cmd.split(" ")[1];
            File saveFile;
            if (cmd.split(" ").length == 3) {
                saveFile = new File(cmd.split(" ")[2]);
            } else {
                saveFile = new File(PathKit.getRootPath() + "/" + fileName);
            }
            cmd = "RETR " + cmd.split(" ")[1];
            FtpSocketUtil.getProxy(ftpSession).getFile(saveFile);
            waitToUnLock();
        } else if (cmd.startsWith("put ")) {
            final File file = new File(cmd.split(" ")[1]);
            String putFile = "";
            if (cmd.split(" ").length == 3) {
                putFile = cmd.split(" ")[2];
            } else {
                putFile = file.getName();
            }
            cmd = "STOR " + putFile;
            FtpSocketUtil.getProxy(ftpSession).putFile(file);
            waitToUnLock();
        } else if ("dir".equals(cmd)) {
            cmd = "LIST";
            FtpSocketUtil.getProxy(ftpSession).printOutStr();
            waitToUnLock();
        } else if (cmd.startsWith("cd ")) {
            cmd = "CWD " + cmd.split(" ")[1];
        } else if ("exit".endsWith(cmd)) {
            System.exit(-1);
        }
        ftpSession.sendMsg(cmd);
    }

    private void waitToUnLock() {
        while (!FtpRequestImpl.getLock()) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String changeToMasterModle() {
        return "PASV";
    }

    @Override
    public String changeToSYST() {
        return "TYPE I";
    }

    public synchronized static void lock() {
        wait = true;
    }

    synchronized static boolean getLock() {
        return wait;
    }

    public synchronized static void unLock() {
        wait = false;
    }
}
